/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.Component;
import org.aswing.ComponentDecorator;
import org.aswing.graphics.Graphics;
import org.aswing.Icon;

import fever.data.libs.BitmapLib;
import fever.data.libs.BitmapLibEvent;
import fever.data.libs.BitmapLibLocator;
import fever.log.FeverDebug;
import fever.utils.Stringifier;

import flash.display.BitmapData;
import flash.filters.BitmapFilter;
import flash.filters.ColorMatrixFilter;
import flash.geom.Point;

/**
 * Use <strong>BitmapData</strong> feature to paint <strong>AsWing Icons</strong>.
 * 
 * @author Romain Ecarnot
 */
class fvaswing.components.FvBitmapIcon extends ComponentDecorator implements Icon
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _lumTransform : ColorMatrixFilter = _initLuminanceTransform();
	
	private var _data : BitmapData;
	private var _disabledData : BitmapData;
	
	private var _width : Number;
	private var _height : Number;
	private var _scale : Boolean;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Creates and returns {@link FvBitmapIcon} using passed-in {@code bitmap id} and 
	 * {@code repository} arguments.
	 * 
	 * @param id Registration bitmap id.
	 * @param repository Locator name
	 */
	public static function create( id : String, repository : String, scale : Boolean ) : FvBitmapIcon
	{
		var locator : BitmapLibLocator = BitmapLibLocator.getLocator( repository );
		
		if( locator.isRegistredBitmap( id ) )
		{
			var bmp : BitmapData = locator.getBitmap( id );
			
			return new FvBitmapIcon( bmp, bmp.width, bmp.height, scale );
		}
		else
		{
			FeverDebug.ERROR( id + ' bitmap is not registred in ' + locator.getLocatorName() );
			return null;
		}
	}
	
	/**
	 * Constructor.
	 * 
	 * @param bmp <strong>BitmapData</strong> to use
	 * @param width ( optional ) Prefered icon's width 	 * @param height ( optional ) Prefered icon's height
	 * @param scale ( optional ) Indicates if <strong>BitmapData</strong> is 
	 * scaled to icon's size. ( default {@code true} ). 
	 */
	public function FvBitmapIcon( bmp : BitmapData, width : Number, height : Number, scale : Boolean )
	{
		_initBitmap( bmp );
		
		_scale = ( scale == undefined ? true : scale );
		
		if( width == undefined )
		{
			width = bmp.width;
			height = bmp.height;
		}
		
		_width = width;
		_height = height;
	}
	
	/**
	 * 
	 */
	public function load( url : String, id : String, repository : String, handler : Function ) : Void
	{
		var lib : BitmapLib = new BitmapLib( BitmapLibLocator.getLocator( repository ).getLocatorName() );
		lib.setName( id );
		lib.addEventListener( BitmapLib.onLoadInitEVENT, this, _onLoadInit, handler );		lib.addEventListener( BitmapLib.onLoadProgressEVENT, this, _onLoadProgress );		lib.addEventListener( BitmapLib.onErrorEVENT, this, _onLoadError );		lib.addEventListener( BitmapLib.onTimeOutEVENT, this, _onLoadError );
		lib.load( url );
	}
	
	/**
	 * Apply the new passed-in filter to <strong>BitmapData</strong> 
	 * instance.
	 * 
	 * <p>Filter is applied to enabled <strong>and</strong> disabled bitmap.  
	 */
	public function applyFilter( f : BitmapFilter ) : Void
	{
		var p : Point = new Point( 0,0 );
		_data.applyFilter( _data, _data.rectangle, p, f );		_disabledData.applyFilter( _disabledData, _disabledData.rectangle, p, f );	
	}
	
	/**
	 * Sets new BitmapData content.
	 * 
	 * @param bmp {@code BitmapData} instance.
	 */
	public function setBitmap( bmp : BitmapData ) : Void
	{
		_initBitmap( bmp );	
	}
	
	/**
	 * Returns {@code BitmapData} content instance.
	 */
	public function getBitmapData(  ) : BitmapData
	{
		return _data;	
	}
	
	/**
	 * Returns the icon's width.
	 */
	public function getIconWidth() : Number
	{
		return _width;
	}
	
	/**
	 * Returns the icon's height.
	 */
	public function getIconHeight() : Number
	{
		return _height;
	}
	
	public function setIconWidth( n : Number ) : Void
	{
		_width = n;
	}
	
	public function setIconHeight( n : Number ) : Void
	{
		_height = n;
	}
	
	/**
	 * Draw the icon at the specified location.
	 */
	public function paintIcon( com : Component, g : Graphics, x : Number, y : Number ) : Void
	{
		var mc : MovieClip = getCreateDecorateMC( com );
		
		if( com.isEnabled() ) mc.attachBitmap( _data, 1 );	
		else mc.attachBitmap( _disabledData, 1 );
		
		mc._x = x;
		mc._y = y;
		
		if( _scale )
		{
			mc._width = getIconWidth();
			mc._height = getIconHeight();
		}
	}
	
	/**
	 * Remove things in the icon object related to the component.
	 * For example remove the MCs created at the first paint time.
	 * 
	 * <p>There is not installIcon method, so you must install icon related things
	 * at the first time of {@code paintIcon}, for example attach a MC if needed.
	 * 
	 * @see #paintIcon()
	 */
	public function uninstallIcon( com : Component ) : Void
	{
		uninstallDecorate( com );
	}
	
	/**
	 * Returns icon's clone using current icon properties.
	 * 
	 * @return {@link FvBitmapIcon} instance
	 */
	public function clone(  ) : FvBitmapIcon
	{
		return new FvBitmapIcon( _data.clone(), _width, _height, _scale );	
	}
	
	/**
	 * String representation of instance
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Creates a static luminance transform matrix.
	 */
	private static function _initLuminanceTransform() : ColorMatrixFilter
	{
		var rwgt : Number = .3086;
		var gwgt : Number = .6094;
		var bwgt : Number = .0820;
 
		var lumTransform : ColorMatrixFilter = new ColorMatrixFilter( 
			new Array (
				rwgt,   gwgt,   bwgt,   0.0,   0.0,
		        rwgt,   gwgt,   bwgt,   0.0,   0.0,
		        rwgt,   gwgt,   bwgt,   0.0,   0.0,
		        0.0,   	0.0,    0.0,   	1.0,   0.0
			)
		);
		return lumTransform;
	}
	
	private function _initBitmap( bmp : BitmapData ) : Void 
	{
		if( _data )
		{
			_data.dispose();			_disabledData.dispose();
		}
		
		_data = bmp.clone();
		_disabledData = _data.clone(); 
		_disabledData.applyFilter( _disabledData, _disabledData.rectangle, new Point( 0,0), BitmapFilter( _lumTransform ) );
	}
	
	private function _onLoadInit( e : BitmapLibEvent, handler : Function ) : Void
	{
		if( e.getBitmap() )	setBitmap( e.getBitmap() );
		if( handler ) handler();
	}
	
	private function _onLoadProgress( e : BitmapLibEvent ) : Void
	{
		
	}

	private function _onLoadError( e : BitmapLibEvent ) : Void
	{
		
	}
}
